package com.ruoyi.tt.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tt.domain.TtArticleComment;
import com.ruoyi.tt.service.ITtArticleCommentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论列表Controller
 *
 * @author ruoyi
 * @date 2022-10-17
 */
@RestController
@RequestMapping("/tt/comment")
public class TtArticleCommentController extends BaseController {
    @Autowired
    private ITtArticleCommentService ttArticleCommentService;


//    @GetMapping("getCommentByArticleId/{articleId}")
//    public AjaxResult getCommentByArticleId(@PathVariable("articleId") Integer articleId) {
//        ttArticleCommentService.getCommentByArticleId(articleId);
//    }

    /**
     * 查询评论列表列表
     */
    @PreAuthorize("@ss.hasPermi('tt:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(TtArticleComment ttArticleComment) {
        startPage();
        List<TtArticleComment> list = ttArticleCommentService.selectTtArticleCommentList(ttArticleComment);
        return getDataTable(list);
    }

    /**
     * 导出评论列表列表
     */
    @PreAuthorize("@ss.hasPermi('tt:comment:export')")
    @Log(title = "评论列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TtArticleComment ttArticleComment) {
        List<TtArticleComment> list = ttArticleCommentService.selectTtArticleCommentList(ttArticleComment);
        ExcelUtil<TtArticleComment> util = new ExcelUtil<TtArticleComment>(TtArticleComment.class);
        util.exportExcel(response, list, "评论列表数据");
    }

    /**
     * 获取评论列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('tt:comment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ttArticleCommentService.selectTtArticleCommentById(id));
    }

    /**
     * 新增评论列表
     */
//    @PreAuthorize("@ss.hasPermi('tt:comment:add')")
    @Log(title = "评论列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TtArticleComment ttArticleComment) {
        System.out.println("1");
        Long userId = SecurityUtils.getUserId();
        ttArticleComment.setUserId(userId);
        ttArticleComment.setCreateTime(new Date());
        return toAjax(ttArticleCommentService.insertTtArticleComment(ttArticleComment));
    }

    /**
     * 修改评论列表
     */
    @PreAuthorize("@ss.hasPermi('tt:comment:edit')")
    @Log(title = "评论列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TtArticleComment ttArticleComment) {
        return toAjax(ttArticleCommentService.updateTtArticleComment(ttArticleComment));
    }

    /**
     * 删除评论列表
     */
//    @PreAuthorize("@ss.hasPermi('tt:comment:remove')")
    // 默认微信创建的用户并没有这个权限
    @Log(title = "评论列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            System.out.println("id = " + id);
        }
        return toAjax(ttArticleCommentService.deleteTtArticleCommentByIds(ids));
    }
}
